package com.imoblife.brainwavestus.customview;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.adapter.ProductBuyAdapter.ProductBuyViewHolder;
import com.imoblife.brainwavestus.database.SQLiteManager;
import com.imoblife.brainwavestus.database.SQLiteManager.DownTaskTable;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItemPragrame;
import com.imoblife.brainwavestus.oldMedia.MediaplayerFactory;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.service.DownLoadSchedulerSystem;
import com.imoblife.brainwavestus.util.AlertDialogUtil;
import com.imoblife.brainwavestus.util.DBUtil;
import com.imoblife.brainwavestus.util.DebugUtil;

public class ListViewItem {
	
	public final String TAG = "ListViewItem";

	public String mTaskName;
	public String mPrice;
	public String mPirceTemp;
	public Button mBuyButton;
	public Button mFreeGet;
	public CheckBox mPrewBox;
	public ProgressBar mDownloadBar;
	public TextView mDownloadtext; 
	public Store mStore;
	public View mItemView;
	public Activity mActivity;
	public DBUtil mDb;
	public Dialog subscrDialog;
	public View purchaseSubScrButton;
	public LinearLayout buyButton;
	public TextView musicName;
	public TextView musicPrice;
	public int mTaskState;
	
	public ListViewItem(String productId,
			ProductBuyViewHolder holder,
			Activity activity) {
		
		mActivity = activity;
		
		mDb = new DBUtil(mActivity);
		
		mTaskName = productId;
		mBuyButton = holder.btBuy;
		mFreeGet = holder.btGetFree;
		mPrewBox = holder.cbProductAudition;
		mDownloadBar = holder.pb;
		mDownloadtext = holder.pbLength;
	}
	
	public void setStore(Store store){
		
		mStore = store;
	}
	
	private boolean checkPlay() {

		try {
			if (new MediaplayerFactory(mActivity).getBackgoundMediaPlayer()
					.isPlaying()) {
				new AlertDialogUtil(mActivity).showAlertDialog(
						mActivity.getResources().getString(
								R.string.alertutil_title),
						mActivity.getResources().getString(
								R.string.alertutil_content_stopmusic),
						mActivity.getResources().getString(
								R.string.alertutil_dismiss));

				return true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return false;

	}
	
	private String getPlayTaskName() {
		SharedPreferences preferences = mActivity.getSharedPreferences(
				"musicName", 0);
		return preferences.getString("playTaskName", "");

	}
	
	public void updateUI() {
		
		Product product = mStore.getProductByName(mTaskName);
		ProductInfo info = product.getProductInfo();
		
		DebugUtil.debug(TAG, "update ui itemName: " + mTaskName);
		if(info.isProductCollection()) {
			return;
		}
		
		DebugUtil.debug(TAG, "item info >>  updateUI");
		
		mDb.openForRead();
		mTaskState = mDb.getDownTaskState(mTaskName + Constant.MUSIC_FILE_SUFFIX);
		mDb.closeDb();
		
		DebugUtil.debug(TAG, "name: " + mTaskName+ " download state: " + mTaskState);
		
		switch (mTaskState) {

		case Constant.INT_DOWN_STATE_NOT_ENBLE: {

			mBuyButton.setText(R.string.purchased);
			((View) mDownloadBar.getParent()).setVisibility(View.GONE);
			mFreeGet.setVisibility(View.VISIBLE);
		}
			break;
		case Constant.INT_DOWN_STATE_END: {
			if (getPlayTaskName().equals(mTaskName)) {
				mBuyButton.setText(R.string.playing);
				mBuyButton.setEnabled(false);
			} else {
				mBuyButton.setText(R.string.reDownload);
				mBuyButton.setEnabled(true);
			}

			((View) mDownloadBar.getParent()).setVisibility(View.GONE);
			mFreeGet.setVisibility(View.GONE);

		}
			break;

		case Constant.INT_DOWN_STATE_CANCEL: {

			mBuyButton.setText(R.string.download);
			((View) mDownloadBar.getParent()).setVisibility(View.GONE);
			mFreeGet.setVisibility(View.GONE);

		}
			break;

		case Constant.INT_DOWN_STATE_FAILE: {

			((View) mDownloadBar.getParent()).setVisibility(View.VISIBLE);
			mBuyButton.setText(R.string.retry);
			mDownloadBar.setMax(100);
			float progress = DownLoadSchedulerSystem.getProgressDownload(
					mTaskName + Constant.MUSIC_FILE_SUFFIX, 
						mActivity);
			
			System.out.println("progress = " + progress);

			int rspCode = mDb.openForRead()
					.getDownFileRspCode(mTaskName 
							+ Constant.MUSIC_FILE_SUFFIX);
			mDb.closeDb();

			if (rspCode == DownLoadItemPragrame.EXTR_FAILE_SDCARDE) {
				mDownloadtext.setText((int) progress + "."
						+ ((int) (progress * 100)) % 100 + "%" + "--"
						+ mActivity.getString(R.string.sdcard_error_extr));
			} else if (rspCode == DownLoadItemPragrame.EXTR_FAILE_NET) {
				mDownloadtext.setText((int) progress + "."
						+ ((int) (progress * 100)) % 100 + "%" + "--"
						+ mActivity.getString(R.string.net_error_extr));
			}

			mDownloadBar.setProgress((int) (progress + 0.5));
			mFreeGet.setVisibility(View.GONE);
		}
			break;
		case Constant.INT_DOWN_STATE_START: {
			((View) mDownloadBar.getParent()).setVisibility(View.VISIBLE);
			mBuyButton.setText(R.string.cancel);
			mDownloadBar.setMax(100);
			float progress = DownLoadSchedulerSystem.getProgressDownload(
					mTaskName + ".music", mActivity);
			String wait = mActivity.getString(R.string.wait);
			mDownloadtext.setText((int) progress + "."
					+ ((int) (progress * 100)) % 100 + "%" + "--" + wait);
			mDownloadBar.setProgress((int) (progress + 0.5));
			DebugUtil.debug(TAG, mTaskName + "progress>>"
					+ ((int) (progress + 0.5)));
							
			mFreeGet.setVisibility(View.GONE);
		}
			break;

		default:
			break;
		}

	}
}
